from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

class ProductDetailPage:
    def __init__(self, driver):
        self.driver = driver
        # 页面元素定位器
        self.product_name_locator = (By.ID, "product-name")
        self.product_description_locator = (By.ID, "product-description")
        self.product_price_locator = (By.ID, "product-price")
        self.add_to_cart_button_locator = (By.ID, "add-to-cart-button")
        self.quantity_selector_locator = (By.ID, "quantity-selector")
        self.customer_reviews_locator = (By.ID, "customer-reviews")
        self.review_rating_locator = (By.ID, "review-rating")
        self.related_products_locator = (By.ID, "related-products")
        self.specifications_locator = (By.ID, "specifications")
        self.share_buttons_locator = (By.ID, "share-buttons")

    def navigate(self, product_id):
        self.driver.get(f'http://example.com/product/{product_id}')

    def get_product_name(self):
        product_name = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.product_name_locator)
        )
        return product_name.text

    def get_product_description(self):
        product_description = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.product_description_locator)
        )
        return product_description.text

    def get_product_price(self):
        product_price = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.product_price_locator)
        )
        return product_price.text

    def add_to_cart(self):
        add_to_cart_button = WebDriverWait(self.driver, 10).until(
            EC.element_to_be_clickable(self.add_to_cart_button_locator)
        )
        add_to_cart_button.click()

    def set_quantity(self, quantity):
        quantity_selector = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.quantity_selector_locator)
        )
        # Implement the logic to set the quantity
        # For example, sending keys to the input field

    def get_customer_reviews(self):
        customer_reviews = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.customer_reviews_locator)
        )
        return [review.text for review in customer_reviews]

    def get_review_rating(self):
        review_rating = WebDriverWait(self.driver, 10).until(
            EC.visibility_of_element_located(self.review_rating_locator)
        )
        return review_rating.text

    def get_related_products(self):
        related_products = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.related_products_locator)
        )
        return [product.text for product in related_products]

    def get_specifications(self):
        specifications = WebDriverWait(self.driver, 10).until(
            EC.presence_of_element_located(self.specifications_locator)
        )
        return specifications.text

    def click_share_button(self, social_media):
        share_buttons = WebDriverWait(self.driver, 10).until(
            EC.presence_of_all_elements_located(self.share_buttons_locator)
        )
        for button in share_buttons:
            if social_media in button.text:
                button.click()
                break

    def verify_share_popup(self, social_media):
        # Implement the logic to verify that the share popup is displayed
        pass